package com.mc2.docapp.disabilityassessmenttool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_LL_active_rom;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llca_ankleFoot;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llca_hip;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.C_llca_knee;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.LLCustDataType;
import com.mc2.docapp.disabilityassessmenttool.LowerLimb.LLInnerCalculations;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class LowrLmbActiveRom extends Activity {

    private static final int id = Activities.LOWER_LIMB_AC_ROM;

    public C_llca_ankleFoot c_llca_ankleFoot = new C_llca_ankleFoot();
    public C_llca_hip c_llca_hip = new C_llca_hip();
    public C_llca_knee c_llca_knee = new C_llca_knee();

    private EditText hipFlexionExtensionArcLeft;
    private EditText hipFlexionExtensionArcRight;
    private EditText hipInternalExternalRotationArcLeft;
    private EditText hipInternalExternalRotationArcRight;
    private EditText hipAdductionAbductionArcLeft;
    private EditText hipAdductionAbductionArcRight;
    private EditText kneeFlexionExtensionArcLeft;
    private EditText kneeFlexionExtensionArcRight;
    private EditText anklefootPlantarDorsiArcLeft;
    private EditText anklefootPlantarDorsiArcRight;
    private EditText anklefootInvEversionLeft;
    private EditText anklefootInvEversionRight;

    //db instance
    private Realm realm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lowr_lmb_active_rom);


        hipFlexionExtensionArcLeft = (EditText) findViewById(R.id.hipFlexionExtensionArcLeft);
        hipFlexionExtensionArcRight = (EditText) findViewById(R.id.hipFlexionExtensionArcRight);
        hipInternalExternalRotationArcLeft = (EditText) findViewById(R.id.hipInternalExternalRotationArcLeft);
        hipInternalExternalRotationArcRight = (EditText) findViewById(R.id.hipInternalExternalRotationArcRight);
        hipAdductionAbductionArcLeft = (EditText) findViewById(R.id.hipAdductionAbductionArcLeft);
        hipAdductionAbductionArcRight = (EditText) findViewById(R.id.hipAdductionAbductionArcRight);
        kneeFlexionExtensionArcLeft = (EditText) findViewById(R.id.kneeFlexionExtensionArcLeft);
        kneeFlexionExtensionArcRight = (EditText) findViewById(R.id.kneeFlexionExtensionArcRight);
        anklefootPlantarDorsiArcLeft = (EditText) findViewById(R.id.anklefootPlantarDorsiArcLeft);
        anklefootPlantarDorsiArcRight = (EditText) findViewById(R.id.anklefootPlantarDorsiArcRight);
        anklefootInvEversionLeft = (EditText) findViewById(R.id.anklefootInvEversionLeft);
        anklefootInvEversionRight = (EditText) findViewById(R.id.anklefootInvEversionRight);

        initRealmDB();
        loadPrevData();
        santizeInput();
        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //HIP
                c_llca_hip.setFlexion_extension_arc(new LLCustDataType(Integer.parseInt(IfNull(hipFlexionExtensionArcRight.getText().toString(), "140")),
                        Integer.parseInt(IfNull(hipFlexionExtensionArcLeft.getText().toString(), "140")), 140d, 0, 0));

                c_llca_hip.setInternal_external_rotation_arc(new LLCustDataType(Integer.parseInt(IfNull(hipInternalExternalRotationArcRight.getText().toString(), "90")),
                        Integer.parseInt(IfNull(hipInternalExternalRotationArcLeft.getText().toString(), "90")), 90d, 0, 0));

                c_llca_hip.setAdduction_abduction_arc(new LLCustDataType(Integer.parseInt(IfNull(hipAdductionAbductionArcRight.getText().toString(), "90")),
                        Integer.parseInt(IfNull(hipAdductionAbductionArcLeft.getText().toString(), "90")), 90d, 0, 0));


                c_llca_hip.calc(c_llca_hip);

                //Knee
                c_llca_knee.setFlexion_extension_arc(new LLCustDataType(Integer.parseInt(IfNull(kneeFlexionExtensionArcRight.getText().toString(), "125")),
                        Integer.parseInt(IfNull(kneeFlexionExtensionArcLeft.getText().toString(), "125")), 125d, 0, 0));

                c_llca_knee.calc(c_llca_knee);

                //ankle foot
                c_llca_ankleFoot.setPlantar_dorsiflection_arc(new LLCustDataType(Integer.parseInt(IfNull(anklefootPlantarDorsiArcRight.getText().toString(), "70")),
                        Integer.parseInt(IfNull(anklefootPlantarDorsiArcLeft.getText().toString(), "70")), 70d, 0, 0));
                c_llca_ankleFoot.setInversion_eversion(new LLCustDataType(Integer.parseInt(IfNull(anklefootInvEversionRight.getText().toString(), "60")),
                        Integer.parseInt(IfNull(anklefootInvEversionLeft.getText().toString(), "60")), 60d, 0, 0));

                c_llca_ankleFoot.calc(c_llca_ankleFoot);

                C_LL_active_rom c_ll_active_rom = new C_LL_active_rom();
                c_ll_active_rom.setC_llca_hip(c_llca_hip);
                c_ll_active_rom.setC_llca_knee(c_llca_knee);
                c_ll_active_rom.setC_llca_ankleFoot(c_llca_ankleFoot);
                //
                LLInnerCalculations in = new LLInnerCalculations();
                in.setSummation_left(c_ll_active_rom.getC_llca_hip().getInner_calculations().getSummation_left() +
                        c_ll_active_rom.getC_llca_knee().getInner_calculations().getSummation_left() +
                        c_ll_active_rom.getC_llca_ankleFoot().getInner_calculations().getSummation_left());

                in.setSummation_right(c_ll_active_rom.getC_llca_hip().getInner_calculations().getSummation_right() +
                        c_ll_active_rom.getC_llca_knee().getInner_calculations().getSummation_right() +
                        c_ll_active_rom.getC_llca_ankleFoot().getInner_calculations().getSummation_right());

                c_ll_active_rom.setInnerCalculations(in);

                NewPatient.finalDto.getClass_lowerLimb_component().setLowerLimb_active_rom_degrees(c_ll_active_rom);
                //
                try {
//                    System.out.println(new ObjectMapper().writeValueAsString(c_active_rom));
                } catch (Exception e) {
                }
                System.out.println();
                c_ll_active_rom.setId(id);
                realm.beginTransaction();
                realm.deleteAll();
                realm.insert(c_ll_active_rom);
                realm.commitTransaction();

               Intent intent = new Intent(LowrLmbActiveRom.this, router(LowrLmbActiveRom.class));
                startActivity(intent);

            }

        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });


    }


    public String IfNull(String value, String defaultValue) {
        if (value.isEmpty() || value == null) {
            return defaultValue;
        } else {
            return value;
        }
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        // realm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration.Builder().name(id+".realm").build();
        realm = Realm.getInstance(config);
    }

    private void loadPrevData() {
        final RealmResults<C_LL_active_rom> dtos = realm.where(C_LL_active_rom.class).findAll();
//        .equalTo("id",id).findAll();
        System.out.println();
        try {
            //apply ui
            //
            //mapUI(new C_LL_active_rom().strip(dtos.get(0)));
            //
            //realm.beginTransaction();
            //dtos.get(0).deleteFromRealm();
            //realm.commitTransaction();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            //no prev data found
        }
    }

    private void mapUI(C_LL_active_rom oldData) {
        hipFlexionExtensionArcLeft.setText(String.valueOf((long) (oldData.getC_llca_hip().getFlexion_extension_arc().getActual_left())));
        hipFlexionExtensionArcRight.setText(String.valueOf((long) (oldData.getC_llca_hip().getFlexion_extension_arc().getActual_right())));

        hipInternalExternalRotationArcLeft.setText(String.valueOf((long) (oldData.getC_llca_hip().getInternal_external_rotation_arc().getActual_left())));
        hipInternalExternalRotationArcRight.setText(String.valueOf((long) (oldData.getC_llca_hip().getInternal_external_rotation_arc().getActual_right())));

        hipAdductionAbductionArcLeft.setText(String.valueOf((long) (oldData.getC_llca_hip().getAdduction_abduction_arc().getActual_left())));
        hipAdductionAbductionArcRight.setText(String.valueOf((long) (oldData.getC_llca_hip().getAdduction_abduction_arc().getActual_right())));

        kneeFlexionExtensionArcLeft.setText(String.valueOf((long) (oldData.getC_llca_knee().getFlexion_extension_arc().getActual_left())));
        kneeFlexionExtensionArcRight.setText(String.valueOf((long) (oldData.getC_llca_knee().getFlexion_extension_arc().getActual_right())));

        anklefootPlantarDorsiArcLeft.setText(String.valueOf((long) (oldData.getC_llca_ankleFoot().getPlantar_dorsiflection_arc().getActual_left())));
        anklefootPlantarDorsiArcRight.setText(String.valueOf((long) (oldData.getC_llca_ankleFoot().getPlantar_dorsiflection_arc().getActual_right())));

        anklefootInvEversionLeft.setText(String.valueOf((long) (oldData.getC_llca_ankleFoot().getInversion_eversion().getActual_left())));
        anklefootInvEversionRight.setText(String.valueOf((long) (oldData.getC_llca_ankleFoot().getInversion_eversion().getActual_right())));

    }

    public void santizeInput() {
        hipFlexionExtensionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "140")});
        hipFlexionExtensionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "140")});

        hipInternalExternalRotationArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "90")});
        hipInternalExternalRotationArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "90")});

        hipAdductionAbductionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "90")});
        hipAdductionAbductionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "90")});

        kneeFlexionExtensionArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "125")});
        kneeFlexionExtensionArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "125")});

        anklefootPlantarDorsiArcLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "70")});
        anklefootPlantarDorsiArcRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "70")});

        anklefootInvEversionLeft.setFilters(new InputFilter[]{new InputFilterMinMax("0", "60")});
        anklefootInvEversionRight.setFilters(new InputFilter[]{new InputFilterMinMax("0", "60")});
    }
}
